package com.dhcc.bpm.modules.scf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.utils.StringUtils;
import com.dhcc.bpm.modules.scf.dto.ScfDriverInfoDTO;
import com.dhcc.bpm.modules.scf.entity.ScfDriverInfo;
import com.dhcc.bpm.modules.scf.service.IScfDriverInfoService;
import com.dhcc.bpm.modules.scf.vo.ScfDriverInfoVO.ScfGetDriverDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfDriverInfoVO.ScfGetDriverListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;


/**
 * 司机管理控制器
 * liuguoting
 * 2023-09-12
 */
@RestController
@AllArgsConstructor
public class ScfDriverInfoController {

    private IScfDriverInfoService scfDriverInfoService;


    /**
     * 查询司机列表
     *
     * @param scfDriverInfo
     * @param query
     * @return
     */
    @GetMapping("/scfGetDriverList")
    // @Cacheable(value = "scfDriverListCache", key = "'scfDriverListCacheKey-' + #scfDriverInfo.hashCode() + '-' + #query.hashCode()", unless = "#result == null")
    public R<IPage<ScfGetDriverListVO>> page(ScfDriverInfoDTO scfDriverInfo, Query query) {

        if (StringUtils.isEmpty( scfDriverInfo.getOrgCode() ) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }
        scfDriverInfo.setOrgCodes(  scfDriverInfo.getOrgCode().split(",") );


        if(scfDriverInfo.getOrgCode().contains( "50000000")){
            scfDriverInfo.setOrgCode("50000000");
            scfDriverInfo.setOrgCodes("50000000".split(",") );
        }

        return R.data(scfDriverInfoService.selectScfDriverInfoPage(Condition.getPage(query), scfDriverInfo));
    }

    /**
     * 查询司机详情
     *
     * @param scfDriverInfo
     * @return
     */
    @GetMapping("/scfGetDriverDetails")
    // @Cacheable(value = "scfDriverDetailsCache", key = "'scfDriverDetailsCacheKey-' + #scfDriverInfo.rowId")
    public R<ScfGetDriverDetailsVO> details(ScfDriverInfoDTO scfDriverInfo) {
        return R.data(scfDriverInfoService.getDetails(scfDriverInfo));
    }

    /**
     * 新增司机信息
     *
     * @param scfDriverInfo
     * @return
     */
    @PostMapping("scfAddDriverDetails")
    // @CacheEvict(value = "scfDriverListCache", allEntries = true)
    public R<Boolean> saveDriver(@RequestBody ScfDriverInfoDTO scfDriverInfo) throws Exception {
        return R.status(scfDriverInfoService.saveDriver(scfDriverInfo));
    }

    /**
     * 修改司机信息
     *
     * @param scfDriverInfo
     * @return
     */
    @PostMapping("scfModDriverDetails")
    // @CacheEvict(value = "scfDriverDetailsCache", key = "'scfDriverDetailsCacheKey-' + #scfDriverInfo.rowId")
    public R<Boolean> updateDrtver(@RequestBody ScfDriverInfoDTO scfDriverInfo) {
        return R.status(scfDriverInfoService.updateDrtver(scfDriverInfo));
    }

    /**
     * 批量导入司机信息
     *
     * @param operatorId 上传用户的id
     * @param file       文件
     * @return
     * @throws IOException
     */
    @PostMapping("/scfDriverBatchFile")
    // @CacheEvict(value = "scfFileInfoListCache", allEntries = true)
    public R<Boolean> uploadFile(@RequestParam("operatorId") String operatorId, MultipartFile file) throws Exception {
        // 获取到批量导入的数据集合
        List<ScfDriverInfo> scfDriverInfos = scfDriverInfoService.batchSaveScfDriverInfo(operatorId, file);
        int bachSaveDriver = scfDriverInfoService.bachSaveDriver(scfDriverInfos, operatorId);
        int total = scfDriverInfos.size();
        int totalSussess = bachSaveDriver;
        int totalFail = total - totalSussess;
        String msg = "解析到" + total + "条数据，" + "导入成功" + totalSussess + "条，导入失败" + totalFail + "条！";
        return R.success(msg);
    }


    @PostMapping("/scfGetDriver/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {

        String[] split = ids.split(",");

        return R.status(scfDriverInfoService.deleteByIds( split ));
    }
}
